package com.paishen.peiniwan.module.message;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.domain.EaseUser;
import com.hyphenate.easeui.utils.EaseUserUtils;
import com.hyphenate.exceptions.HyphenateException;
import com.paishen.common.util.BaseUtil;
import com.paishen.common.widget.TitleLayout;
import com.paishen.peiniwan.App;
import com.paishen.peiniwan.BaseActivity;
import com.paishen.peiniwan.R;

import java.util.ArrayList;
import java.util.List;

public class EaseBlackListActivity extends BaseActivity {
    private RecyclerView viewRecycler;
    private BlackListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ease_black_list);

        TitleLayout layTitle = (TitleLayout) findViewById(R.id.lay_title);
        layTitle.pushContent(makeBackView(), getString(R.string.blacklist), null, false);

        viewRecycler = (RecyclerView) findViewById(R.id.view_recycler);
        viewRecycler.setLayoutManager(new GridLayoutManager(this, 4));
        viewRecycler.setAdapter(adapter = new BlackListAdapter());
        viewRecycler.setItemAnimator(new DefaultItemAnimator());


        // 从本地获取黑名单
        List<EaseUser> dataList = new ArrayList<>();
        for (String easeUsername : EMClient.getInstance().contactManager().getBlackListUsernames()) {
            dataList.add(EaseUserUtils.getUserInfo(easeUsername));
        }
        adapter.set(dataList);
    }


    private static final class BlackListAdapter extends RecyclerView.Adapter<BlackListAdapter.ViewHolder> {

        private AlertDialog alertDialog;
        List<EaseUser> elements = new ArrayList<>();

        public void set(List<EaseUser> dataList) {
            elements.clear();
            elements.addAll(dataList);
            notifyItemRangeChanged(0, getItemCount());
        }

        public void remove(EaseUser user) {
            int idx = elements.indexOf(user);
            notifyItemRemoved(idx);
            elements.remove(user);
        }

        public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
            public ViewHolder(View view) {
                super(view);
                view.setOnClickListener(this);
            }

            EaseUser user;
            private TextView txNickname;
            private ImageView imgCover;

            @Override
            public void onClick(View v) {
                showRemoveConfirmDialog(v.getContext(), user);
            }
        }

        @Override
        public int getItemCount() {
            return elements.size();
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recycler_ease_black_list, viewGroup, false);
            ViewHolder viewHolder = new ViewHolder(view);
            viewHolder.txNickname = (TextView) view.findViewById(R.id.tx_nickname);
            viewHolder.imgCover = (ImageView) view.findViewById(R.id.img_cover);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(final ViewHolder viewHolder, final int i) {
            viewHolder.user = elements.get(i);
            viewHolder.txNickname.setText(viewHolder.user.getNick());
            Glide.with(viewHolder.imgCover.getContext()).load(viewHolder.user.getAvatar()).bitmapTransform(App.i().getCropCircleTransformation()).into(viewHolder.imgCover);
        }

        void showRemoveConfirmDialog(final Context context, final EaseUser tobeRemoveUser) {
            alertDialog = new AlertDialog.Builder(context).setTitle(R.string.move_out_backlist)
                    .setCancelable(false).setPositiveButton(R.string.confirm
                            , new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            alertDialog.dismiss();
                            removeOutBlacklist(context, tobeRemoveUser);
                        }
                    }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            alertDialog.dismiss();
                        }
                    }).create();
            alertDialog.show();
        }

        /**
         * 移出黑民单
         *
         * @param tobeRemoveUser
         */
        void removeOutBlacklist(final Context context, final EaseUser tobeRemoveUser) {
            final ProgressDialog pd = new ProgressDialog(context);
            pd.setMessage(context.getString(R.string.be_removing));
            pd.setCanceledOnTouchOutside(false);
            pd.show();
            new Thread(new Runnable() {
                public void run() {
                    try {
                        // 移出黑民单
                        EMClient.getInstance().contactManager().removeUserFromBlackList(tobeRemoveUser.getUsername());
                        ((Activity) context).runOnUiThread(new Runnable() {
                            public void run() {
                                pd.dismiss();
                                remove(tobeRemoveUser);
                            }
                        });
                    } catch (HyphenateException e) {
                        e.printStackTrace();
                        ((Activity) context).runOnUiThread(new Runnable() {
                            public void run() {
                                pd.dismiss();
                                BaseUtil.showToast(R.string.Removed_from_the_failure);
                            }
                        });
                    }
                }
            }).start();
        }
    }
}
